/**
 * Util methods to manipulate strings.
 *
 * @author Jorge Hernandez
 */
class org.molamil.commons.StringUtil {
	
	
	// Constructor
	
	private function StringUtil() {}
	
	
	// Functions
	
	/**
	 * Replaces all occurrences of the given pattern with the given replace text.
	 * 
	 * @param   source Original string.
	 * @param   pattern String to find.
	 * @param   replace String to replace <code>pattern</code> with.
	 * @return  A new String with all occurrences of <code>pattern</code> replaced by <code>replace</code>.
	 */
	public static function replace(source:String, pattern:String, replace:String):String {
		var s:String = "";
        if (source != null) {
	        var len:Number = pattern.length;
	        var found:Number = -1;
			var start:Number = 0;
			while ((found = source.indexOf(pattern, start)) != -1) {
				s += source.substring(start, found);
				s += replace;
				start = found + len;
			}
			s += source.substring(start);
        }
        return s;
	}
	
	
}